package com.nidu.demo.web.system;

import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.fastjson2.JSONArray;
import com.nidu.demo.base.BatchDeleteCmd;
import com.nidu.demo.log.api.SysOperateLogService;
import com.nidu.demo.log.dto.OperateLogCO;
import com.nidu.demo.log.dto.OperateLogPageQry;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;

/**
 * 操作日志管理
 *
 * @author auto
 */
@Tag(name = "管理后台 - 操作日志")
@RestController
@RequestMapping("/system/operate-log")
@Validated
@RequiredArgsConstructor
public class SysOperateLogController {

    private final SysOperateLogService operateLogService;

    @Operation(summary = "获取操作日志详情", description = "根据 ID 获取操作日志详细信息")
    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:operate-log:query')")
    public SingleResponse<OperateLogCO> getOperateLogById(@PathVariable Long id) {
        return operateLogService.getById(id);
    }

    @Operation(summary = "分页查询操作日志", description = "分页获取操作日志信息")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermission('system:operate-log:query')")
    public PageResponse<OperateLogCO> pageOperateLogs(OperateLogPageQry qry) {
        return operateLogService.page(qry);
    }

    @Operation(summary = "删除操作日志", description = "根据 ID 删除操作日志")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:operate-log:delete')")
    public Response deleteOperateLog(@PathVariable Long id) {
        return operateLogService.delete(id);
    }

    @Operation(summary = "批量删除操作日志", description = "根据 ID 列表删除多个操作日志")
    @PostMapping("/batch-delete")
    @PreAuthorize("@ss.hasPermission('system:operate-log:delete')")
    public Response deleteOperateLogs(@RequestBody BatchDeleteCmd cmd) {
        List<Long> ids = JSONArray.parseArray(cmd.getDelete(), Long.class);
        return operateLogService.delete(new HashSet<>(ids));
    }

    @Operation(summary = "清空操作日志", description = "清空所有操作日志")
    @PostMapping("/clear")
    @PreAuthorize("@ss.hasPermission('system:operate-log:delete')")
    public Response clearOperateLogs() {
        return operateLogService.clear();
    }

    @Operation(summary = "导出操作日志", description = "导出操作日志到Excel")
    @PostMapping("/export")
    @PreAuthorize("@ss.hasPermission('system:operate-log:export')")
    public Response exportOperateLogs(@RequestBody OperateLogPageQry qry) {
        return operateLogService.export(qry);
    }
}
